package com.mkb.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mkb.dao.GraphCaseDAO;
import com.mkb.entity.GraphCaseDO;
import com.mkb.entity.GraphCaseDTO;
import com.mkb.service.GraphCaseService;
import com.mkb.utils.MapUtils;
import com.zz.common.util.DateUtils;
import com.zz.common.util.ObjectUtils;
import com.zz.common.validator.Assert;
import com.zz.common.validator.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class GraphCaseServiceImpl implements GraphCaseService {
	@Autowired
	private GraphCaseDAO graphCaseDAO;

	@Override
	public Boolean deleteById(Long id) {
		graphCaseDAO.deleteById(id);
		return true;
	}

	@Override
	public Long create(GraphCaseDTO record) throws Exception {
		GraphCaseDO entity = record.clone(GraphCaseDO.class);
		graphCaseDAO.insert(entity);
		return entity.getId();
	}

	@Override
	public GraphCaseDTO getById(Long id) throws Exception{
		Assert.isNull(id, "id 不能为空");
		GraphCaseDO entity= graphCaseDAO.getById(id);
		Assert.isNull(entity, "对象不存在");
		GraphCaseDTO dto = entity.clone(GraphCaseDTO.class);
		return dto;
	}

	@Override
	public Boolean update(GraphCaseDTO record) throws Exception {
		Assert.isNull(record, "对象不存在");
		Assert.isNull(record.getId(), "对象id不存在");
		GraphCaseDO entity = record.clone(GraphCaseDO.class);
		graphCaseDAO.update(entity);
		return true;
	}

	@Override
	public PageInfo<GraphCaseDTO> queryPage(GraphCaseDTO graphCaseDTO) throws Exception {
	        ValidatorUtils.validateProperty(graphCaseDTO, "pageNum", "pageSize");
		PageHelper.startPage(graphCaseDTO.getPageNum(), graphCaseDTO.getPageSize());
		List<GraphCaseDO> graphCaseDOS = graphCaseDAO.queryList(MapUtils.parseMap(graphCaseDTO));
		List<GraphCaseDTO> graphCaseDTOList = ObjectUtils.convertList(graphCaseDOS, GraphCaseDTO.class);
		for (GraphCaseDTO record : graphCaseDTOList){
			record.setCaseDateStr(record.getCaseDate()== null ? null : DateUtils.formatDate(record.getCaseDate().getTime(), DateUtils.DATE_FROMAT1));
			record.setCriminalDateStr(record.getCriminalDate()== null ? null : DateUtils.formatDate(record.getCriminalDate().getTime(), DateUtils.DATE_FROMAT1));
		}
		return new PageInfo<>(graphCaseDTOList);
	}

	@Override
	public List<GraphCaseDTO> queryList(GraphCaseDTO graphCaseDTO) throws Exception {
		List<GraphCaseDO> graphCaseDOS = graphCaseDAO.queryList(MapUtils.parseMap(graphCaseDTO));
		List<GraphCaseDTO> graphCaseDTOList = ObjectUtils.convertList(graphCaseDOS, GraphCaseDTO.class);
		return graphCaseDTOList;
	}

	@Override
	public int count(Map<String, Object> map) throws Exception {
		return graphCaseDAO.count(map);
	}

	@Override
	public int deleteByGraphId(Long graphId) throws Exception {
		return graphCaseDAO.deleteByGraphId(graphId);
	}

	@Override
	public void createBatch(List<GraphCaseDTO> graphCaseList) throws Exception{
		graphCaseDAO.insertBatch(ObjectUtils.convertList(graphCaseList, GraphCaseDO.class));
	}

}